<?
/**
 * 新闻
 */
class Brand extends MyBase{
	
	function __construct() {
		parent::__construct('brand.id', array('keyword' => array('name', 'mark')));
	}
	
	/**
	 * 修改新闻
	 */
	public function edit($post){
		$id = intval($post['id']);
		$select = array('name' => $post['name']);
		if($id > 0){
			$select['kept'] = " AND `id`!={$id}";
		}
		$rs = $this->db->get_row($this->tbname, $select, 'id');
		if(!empty($rs)){
			return $this->rwarn(lang('already') . lang('exist'));
		}
		if($id > 0){
			$brand = $this->get_row($id, 'id,mark,addtime');
		}
		if ($post['url'] == 'http://') {
			$post['url'] = '';
		}
		$data = array(
			'name'			=> $post['name'],
			'mark'			=> $post['mark'],
			'url'			=> $post['url'],
			'rank'			=> $post['rank'],
			'content'		=> $post['content'],
			'related_news'	=> $post['related_news'],
		);

		$dellogo = empty($post['dellogo']) ? 0 : 1;
		if($dellogo == 1){
			if(!empty($post['logoold'])){
				unlink(UP_PTH.$post['logoold']);
				unlink(UP_PTH.'thumb/'.$post['logoold']);
			}
			$data['logo'] = '';
		}else{
			// 上传图片
			$oUpload = new Upload(array(
				'upload_form_field'	=> 'logo',
				'auto_date_dir'		=> false,
				'out_file_dir'		=> UP_PTH.'brand/',
				'allowed_file_ext'	=> array('gif', 'jpg', 'jpeg', 'png'),
			));
			$img_arr = $oUpload->upload_process();
			if(!empty($img_arr)){
				// 删除旧图
				if(!empty($post['logoold'])){
					unlink(UP_PTH.$post['logoold']);
					unlink(UP_PTH.'thumb/'.$post['logoold']);
				}
				$logo_path = array_pop($img_arr);
				// 取相对路径
				$data['logo'] = str_replace(UP_PTH, '', $logo_path);
				File::resize($logo_path, 320, 135, 'basic', false);
			}
		}

		if($id > 0){
			$data['updatetime'] = TIME;
			$r = $this->db->update($this->tbname, array('id' => $id), $data);
		}else{
			$data['addtime'] = TIME;
			$r = $id = $this->db->insert($this->tbname, $data);
		}
		if($r > 0){
			if(isset($brand) && !empty($brand)){
				$this->delete_html_file($id, $brand['addtime'], $brand['mark']);
			}
		}
		return $this->cache_brand();
	}

	public function rank($post){
		if(!is_array($post['rank']))$post['rank'] = array($post['rank']);
		$iRtn = 0;
		foreach($post['rank'] as $id => $rank){
			if($this->db->update($this->tbname, array('id' => $id), array(
				'rank'		 => $rank,
				'updatetime' => TIME,
			))){
				$iRtn ++;
			}
		}
		if($iRtn > 0){
			return $this->cache_brand();
		}
		return $iRtn;
	}

	/**
	 * 删除新闻
	 */
	public function del($post){
		$iRtn = 0;
		if(!is_array($post['list']))$post['list'] = array($post['list']);
		foreach($post['list'] as $id){
			$brand = $this->get_row($id, 'id,mark,addtime');
			if(empty($brand) && empty($brand['id']))continue;
			if($this->db->update($this->tbname, array('id' => $id), array('isdel' => 1))){
				$iRtn ++;
			}
			$this->delete_html_file($id, $brand['addtime'], $brand['mark']);
		}
		if($iRtn > 0){
			return $this->cache_brand();
		}
		return $iRtn;
	}

	/*******************************************************************************
	 * 查询
	 *******************************************************************************/

	public function get_by_mark($mark, $fields = '*'){
		return $this->db->get_row($this->tbname, array('mark' => $mark), $fields);
	}
	
	public function get_prev($rank, $id, $fields = '*'){
		$row = $this->db->sql_row("SELECT {$fields} FROM `tb_brand` WHERE `rank`>={$rank} AND `id`>{$id} ORDER BY `rank` ASC,id ASC");
		if(empty($row)){
			$row = $this->db->sql_row("SELECT {$fields} FROM `tb_brand` WHERE `rank`>{$rank} AND `id`<{$id} ORDER BY `rank` ASC,id ASC");
		}
		return $row;
	}
	
	public function get_next($rank, $id, $fields = '*'){
		$row = $this->db->sql_row("SELECT {$fields} FROM `tb_brand` WHERE `rank`<={$rank} AND `id`<{$id} ORDER BY `rank` DESC,id DESC");
		if(empty($row)){
			$row = $this->db->sql_row("SELECT {$fields} FROM `tb_brand` WHERE `rank`<{$rank} AND `id`>{$id} ORDER BY `rank` DESC,id DESC");
		}
		return $row;
	}

	/********************************************************************************
	 * 更新静态文件
	 ********************************************************************************/
	
	public function update_html_files($list){
		if(in_array('brand_content', $list)){
			$brand_content = true;
		}else{
			$brand_content = false;
		}
		foreach ($list as $key){
			if($key == 'index'){
				File::delFile(WEB_PTH.'index.html');
			}elseif($key == 'brand_index'){
				$this->delete_html_list(0, $brand_content);
			}else{
				$this->delete_html_list($key, $brand_content);
			}
		}
		return true;
	}
	
	private function delete_html_file($id, $addtime, $mark = ''){
		if(!empty($mark)){
			File::delFile(PUB_PTH.'brand/'.$mark.'.html');
		}
		File::delFile(PUB_PTH.'brand/'.time_to_path($addtime).'/'.$id.'.html');
	}
	
	private function delete_html_list($key, $brand_content = false){
		$catid = intval(str_replace('brand_category_', '', $key));
		if($catid > 0){
			if($brand_content == true){
				$list = $this->get_list('id,mark,addtime', array('catid' => $catid), array(), 1, 2000);
				if(isset($list['list']) && is_array($list['list'])){
					foreach($list['list'] as $row){
						$this->delete_html_file($row['id'], $row['addtime'], $row['mark']);
					}
				}
			}
			
			$brand_categorys = cache_read('brand_categorys.php');
			$mark = $brand_categorys['category_list'][$catid]['mark'].'/';
		}else{
			$mark = '';
		}
		$files = File::getFiles(PUB_PTH.'brand/'.$mark, false);
		foreach ($files as $file){
			File::delFile(PUB_PTH.'brand/'.$mark.$file);
		}
	}
	
	/********************************************************************************
	 * 缓存
	********************************************************************************/
	
	public function cache_brand(){
		$all = $this->get_all('*', array('isdel' => 0), array('rank' => 'DESC', 'id' => 'DESC'));
		$brands = array();
		if(!empty($all['list'])){
			foreach ($all['list'] as $key => $brand_info) {
				$brands[$brand_info['id']] = array(
					'name'		=> $brand_info['name'],
					'mark'		=> $brand_info['mark'],
					'url'		=> $brand_info['url'],
					'logo'		=> $brand_info['logo'],
				);
			}
		}
		return cache_write('brands.php', $brands);
	}
}
?>